package xyz.csmcool.imroom.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Maps;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import xyz.csmcool.imroom.entity.RoomsUser;
import xyz.csmcool.imroom.entity.vo.ExamineRoomUserVo;
import xyz.csmcool.imroom.entity.vo.ExamineUserFriendVo;
import xyz.csmcool.imroom.entity.vo.RoomMutedVo;
import xyz.csmcool.imroom.entity.vo.RoomUserVo;
import xyz.csmcool.imroom.service.IRoomsUserService;
import xyz.csmcool.imutil.dto.RoomUserDto;
import xyz.csmcool.imutil.dto.UserDto;
import xyz.csmcool.imutil.utils.JwtUtils;
import xyz.csmcool.imutil.utils.ResultTool;
import xyz.csmcool.imutil.webVo.MyPage;
import xyz.csmcool.imutil.webVo.ResultCode;
import xyz.csmcool.imutil.webVo.ResultVo;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author csmcool
 * @since 2022-08-08
 */
@RestController
@RequestMapping("/roomsUser")
@RequiredArgsConstructor
public class RoomsUserController {

    final IRoomsUserService roomsUserService;

    @GetMapping("/room-user/{limit}/{page}/{roomId}")
    public ResultVo<MyPage<UserDto>> getRoomUserInfo(@PathVariable("limit") Long limit,
                                                     @PathVariable("page") Long page,
                                                     @PathVariable("roomId") Integer roomId,
                                                     HttpServletRequest request) {
        String userId = JwtUtils.getMemberIdByJwtToken(request);
        MyPage<UserDto> userDtos = roomsUserService.selectRoomUserInfo(page, limit, roomId, Integer.valueOf(userId));
        return ResultTool.success(userDtos);
    }

    @GetMapping("/room-user/{limit}/{page}/{roomId}/{name}")
    public ResultVo<MyPage<UserDto>> getRoomUserInfoByName(@PathVariable("limit") Long limit,
                                                           @PathVariable("page") Long page,
                                                           @PathVariable("roomId") Integer roomId,
                                                           HttpServletRequest request, @PathVariable("name") String name) {
        String userId = JwtUtils.getMemberIdByJwtToken(request);
        MyPage<UserDto> userDtos = roomsUserService.selectRoomUserInfoByName(page, limit, roomId, Integer.valueOf(userId), name);
        return ResultTool.success(userDtos);
    }

    @GetMapping("/current-room-user/{limit}/{page}/{roomId}")
    public ResultVo<MyPage<UserDto>> getCurrentRoomUserInfo(@PathVariable("limit") Long limit,
                                                            @PathVariable("page") Long page,
                                                            @PathVariable("roomId") Integer roomId,
                                                            HttpServletRequest request) {
        String userId = JwtUtils.getMemberIdByJwtToken(request);
        MyPage<UserDto> currentRoomUsers = roomsUserService.getCurrentRoomUsers(page, limit, roomId, Integer.valueOf(userId));
        return ResultTool.success(currentRoomUsers);
    }

    @GetMapping("/is-room-user")
    public ResultVo<Map<String, Integer>> isRoomUser(@RequestParam("roomId") Integer roomId,
                                                     @RequestParam("uid") Integer uid) {
        Map<String, Integer> map = Maps.newHashMapWithExpectedSize(1);
        Integer integer = roomsUserService.isRoomUserAndMuted(roomId, uid);
        map.put("result", integer == null ? -1 : integer);
        return ResultTool.success(map);
    }

    @PostMapping("/exit-room/{id}")
    public ResultVo exitMyJoinRoom(@PathVariable("id") Integer id, HttpServletRequest request) {
        String uid = JwtUtils.getMemberIdByJwtToken(request);
        roomsUserService.exitCurrentRoom(Integer.valueOf(uid), id);
        return ResultTool.success();
    }

    @PostMapping("/muted-user")
    public ResultVo mutedUser(@RequestBody RoomMutedVo roomMutedVo, HttpServletRequest request) {
        String id = JwtUtils.getMemberIdByJwtToken(request);
        roomsUserService.mutedUser(roomMutedVo.getRoomId(), roomMutedVo.getId(), Integer.valueOf(id));
        return ResultTool.success();
    }

    @PostMapping("/not-muted-user")
    public ResultVo notMutedUser(@RequestBody RoomMutedVo roomMutedVo, HttpServletRequest request) {
        String id = JwtUtils.getMemberIdByJwtToken(request);
        roomsUserService.notMutedUser(roomMutedVo.getRoomId(), roomMutedVo.getId(), Integer.valueOf(id));
        return ResultTool.success();
    }

    @GetMapping("/get-add-room-user/{limit}/{page}")
    public ResultVo<MyPage<RoomUserVo>> getAddMyRoomUser(@PathVariable("limit") Long limit,
                                                         @PathVariable("page") Long page,
                                                         HttpServletRequest request) {
        String id = JwtUtils.getMemberIdByJwtToken(request);
        MyPage<RoomUserVo> addMyRoom = roomsUserService.getAddMyRoom(page, limit, Integer.valueOf(id));
        return ResultTool.success(addMyRoom);
    }

    @PostMapping("/examine-add-room-user")
    public ResultVo examineAddFriend(HttpServletRequest request,
                                     @RequestBody ExamineRoomUserVo examineRoomUserVo) {
        String id = JwtUtils.getMemberIdByJwtToken(request);
        roomsUserService.examineRoomUser(examineRoomUserVo.getUid(),
                examineRoomUserVo.getRoomId(), examineRoomUserVo.getIsPass(),
                Integer.valueOf(id));
        return ResultTool.success();
    }

    @PostMapping("/add-room")
    public ResultVo joinRoom(HttpServletRequest request,
                             @RequestParam("roomId") Integer roomId) {
        String id = JwtUtils.getMemberIdByJwtToken(request);
        roomsUserService.joinRoom(Integer.valueOf(id), roomId);
        return ResultTool.success();
    }

    @PostMapping("/add-room-password")
    public ResultVo joinRoomByPassword(HttpServletRequest request,
                                       @RequestParam("roomId") Integer roomId,
                                       @RequestParam("password") String password) {
        String id = JwtUtils.getMemberIdByJwtToken(request);
        roomsUserService.joinRoomByPassword(Integer.valueOf(id), roomId,password);
        return ResultTool.success();
    }
}
